package org.ssp.test;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;
/**
 *  创建带附件的邮件
 *  
 * @author susp
 *
 */

public class EmailTest3 {
	public static void main(String[] args) {
		Properties prop=new Properties();
		prop.put("mail.host", "smtp.qq.com");
		prop.put("mail.transport.protocol", "smtp");
		prop.put("mail.smtp.auth", "true");
		MailSSLSocketFactory sf=null;
		//ssl加密
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);

		} catch (GeneralSecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		//创建session
		Session session=Session.getInstance(prop);
		//创建transport
		Transport ts = null;
		try {
			ts = session.getTransport();
			//创建message
			Message message=createAttached(session);
			//连接服务器并验证
			ts.connect("smtp.qq.com","1255292979","slhphqvgsujkjbgi");
			//发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			
		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				ts.close();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
	}
	public static MimeMessage createAttached(Session session){
		MimeMessage message=new MimeMessage(session);
		//设置邮件的基本信息
		try {
			message.setFrom(new InternetAddress("1255292979@qq.com"));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress("1255292979@qq.com"));
			message.setSubject("javamail邮件发送测试");
			//创建邮件正文，为了避免中文乱码，需要使用charset=utf-8指定编码
			MimeBodyPart text=new MimeBodyPart();
			text.setContent("创建带附件的邮件","text/html;charset=utf-8");
			
			//创建邮件附件
			MimeBodyPart attach=new MimeBodyPart();
			DataHandler dh=new DataHandler(new FileDataSource("src\\login-bg.jpg"));
			attach.setDataHandler(dh);
			attach.setFileName(dh.getName());
			
			//创建容器描述关系
			MimeMultipart mm=new MimeMultipart();
			mm.addBodyPart(text);
			mm.addBodyPart(attach);
			mm.setSubType("mixed");
			
			message.setContent(mm);
			message.saveChanges();
			return message;
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
		
	}

	

}
